/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stis.statpro.ibs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import stis.statpro.ibs.datasource.Datasource;
import stis.statpro.ibs.model.Provinsi;

/**
 *
 * @author habib <imamhabibpamungkas@gmail.com>
 */
public class ProvinsiDao {

    private List<Provinsi> listProp;

    public List<Provinsi> getALl() throws SQLException {
        listProp = new ArrayList<>();
        Connection c = Datasource.getConnection();
        PreparedStatement p = c.prepareStatement("SELECT * from Provinsi;");
        ResultSet rs = p.executeQuery();
        while (rs.next()) {
            Provinsi prov = new Provinsi();
            prov.setId(rs.getInt("id"));
            prov.setNama(rs.getString("nama"));
            listProp.add(prov);
        }
        p.close();
        return listProp;
    }
    
    public int getId(String nama) throws SQLException{
        int id=0;
        Connection c = Datasource.getConnection();
        PreparedStatement p = c.prepareStatement("SELECT id from Provinsi where nama=?;");
        p.setString(1, nama);
        ResultSet rs = p.executeQuery();
        id=rs.getInt("id");
        p.close();
        return id;
        
    }
}
